﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinformDevFramework.Core.Winform;
using WinformDevFramework.IServices;
using WinformDevFramework.IServices.System;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinformDevFramework.Pages.Systemic
{
    public partial class FrmUserInformation : BaseForm1
    {

        private ISystemSelectServices _sysSelectServices;

        public FrmUserInformation(ISystemSelectServices sysSelectServices)
        {
            _sysSelectServices = sysSelectServices;
            InitializeComponent();
        }

        /// <summary>
        /// 重写初始化
        /// </summary>
        public override void Init()
        {
            //调用基类中的Init
            base.Init();

            //查询数据
            this.dataGridViewList.DataSource= GetData();

            SetToolButtonStatus(formStatus);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        private List<sysSystemSelect> GetData()
        {
            List<sysSystemSelect> data = new List<sysSystemSelect>();
            data=_sysSelectServices.Query();
            return data;
        }

        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DataGridViewListDoubleClick(object? sender, DataGridViewCellEventArgs e)
        {
            base.DataGridViewListDoubleClick(sender, e);
            var g = (DataGridView)sender;
            var model = g.CurrentRow.DataBoundItem as sysSystemSelect;

            //给详情页设置数据
            this.txtID.Text=model.ID.ToString();
            this.txt_SystemName.Text=model.SystemName;

            SetToolButtonStatus(formStatus);
            //SetToolsButton();
        }

        /// <summary>
        /// 设置功能按钮状态
        /// </summary>
        public override void SetToolButtonStatus(FormStatus status)
        {
            switch (status)
            {
                case FormStatus.Add:
                    {
                        btnAdd.Enabled = false;
                        btnEdit.Enabled = false;
                        btnSave.Enabled = true;
                        btnDel.Enabled = false;
                        btnCanel.Enabled = true;
                        btnSearch.Enabled = false;
                        SetControlStatus(this.groupBox1, true);
                        ClearControlsText(this.groupBox1);
                        break;
                    }
                case FormStatus.Edit:
                    {
                        btnAdd.Enabled = false;
                        btnEdit.Enabled = false;
                        btnSave.Enabled = true;
                        btnDel.Enabled = false;
                        btnCanel.Enabled = true;
                        btnSearch.Enabled = false;
                        SetControlStatus(this.groupBox1, true);
                        break;
                    }
                case FormStatus.View:
                    {
                        btnAdd.Enabled = true;
                        btnEdit.Enabled = true;
                        btnSave.Enabled = false;
                        btnDel.Enabled = true;
                        btnCanel.Enabled = false;
                        btnSearch.Enabled = false;
                        SetControlStatus(this.groupBox1, false);
                        break;
                    }
                case FormStatus.Canel:
                    {
                        btnAdd.Enabled = true;
                        btnEdit.Enabled = true;
                        btnSave.Enabled = false;
                        btnDel.Enabled = false;
                        btnCanel.Enabled = false;
                        btnSearch.Enabled = false;
                        SetControlStatus(this.groupBox1, false);
                        break;
                    }
                case FormStatus.First:
                    {
                        btnAdd.Enabled = true;
                        btnEdit.Enabled = false;
                        btnSave.Enabled = false;
                        btnDel.Enabled = false;
                        btnCanel.Enabled=false;
                        btnSearch.Enabled = true;
                        SetControlStatus(this.groupBox1, false);
                        break;
                    }
                case FormStatus.Save:
                    {
                        btnAdd.Enabled = true;
                        btnEdit.Enabled = true;
                        btnSave.Enabled = false;
                        btnDel.Enabled = true;
                        btnCanel.Enabled = false;
                        btnSearch.Enabled = false;
                        SetControlStatus(this.groupBox1, false);
                        break;
                    }
                case FormStatus.Del:
                    {
                        btnAdd.Enabled = true;
                        btnEdit.Enabled = false;
                        btnSave.Enabled = false;
                        btnDel.Enabled = false;
                        btnCanel.Enabled = false;
                        btnSearch.Enabled = false;
                        SetControlStatus(this.groupBox1, false);
                        ClearControlsText(this.groupBox1);
                        break;
                    }
            }
        }

        public override void AddFunction(object sender, EventArgs e)
        {
            base.AddFunction(sender, e);
            SetToolButtonStatus(formStatus);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void EditFunction(object sender, EventArgs e)
        {
            base.EditFunction(sender, e);
            SetToolButtonStatus(formStatus);
            ltUpdateBy.TextBox.Text = AppInfo.User.Fullname;
            ldUpdateDate.DateTimePicker.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //btnEdit.Enabled = true;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void SaveFunction(object sender, EventArgs e)
        {
            base.SaveFunction(sender, e);
            //新增
            if (string.IsNullOrEmpty(this.txtID.Text))
            {
                sysSystemSelect sysSystemSelect = new sysSystemSelect();
                sysSystemSelect.SystemName =this.txt_SystemName.Text;
                sysSystemSelect.SystemUrl =this.txt_SystemUrl.Text;
                sysSystemSelect.SystemType =this.txt_SystemType.Text;

                sysSystemSelect.CreateBy=AppInfo.User.Fullname;
                sysSystemSelect.CreateTime=DateTime.Parse(ldCreateDate.DateTimePicker.Text);
                var id= _sysSelectServices.Insert(sysSystemSelect);
                this.txtID.Text=id.ToString();
                if (id > 0)
                {
                    MessageBox.Show("保存成功！", "提示");
                }
            }
            //修改
            else
            {
                sysSystemSelect sysSystemSelect= _sysSelectServices.QueryById(int.Parse(txtID.Text));
                sysSystemSelect.SystemName =this.txt_SystemName.Text;
                sysSystemSelect.SystemUrl =this.txt_SystemUrl.Text;
                sysSystemSelect.SystemType =this.txt_SystemType.Text;
                sysSystemSelect.UpdateBy = AppInfo.User.Fullname;
                sysSystemSelect.UpdateTime = DateTime.Parse(ldCreateDate.DateTimePicker.Text);

                if (_sysSelectServices.Update(sysSystemSelect))
                {
                    MessageBox.Show("保存成功！", "提示");
                }
            }
        }

        public override bool ValidateData()
        {
            bool result = false;

            //if (!lcMenuType.ComboBox.Text.Equals("Menu")&&string.IsNullOrEmpty(uiComboTreeViewPMenu.Text))
            //{
            //    //MessageBox.Show("父菜单不能为空！");
            //    //return result;
            //}
            result = true;
            return result;
        }
        public override void CanelFunction(object sender, EventArgs e)
        {
            bool isAdd = formStatus == FormStatus.Add;
            base.CanelFunction(sender, e);
            SetToolButtonStatus(formStatus);
            btnEdit.Enabled = !isAdd;
            btnDel.Enabled = !isAdd;
        }

        /// <summary>
        /// 选择图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectIcon_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory=Application.StartupPath + "Resources\\";
            openFileDialog.Filter="Image Files (*.jpg, *.png, *.gif, *.bmp)|*.jpg; *.png; *.gif; *.bmp";
            openFileDialog.Multiselect = false;

            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedImagePath=openFileDialog.FileName.Replace(Application.StartupPath, "");
                this.ltIcon.TextBox.Text= selectedImagePath;
            }
        }
    }
}
